package cz.muni.fi.pa165.sportclubmanager.entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 * Age category is defined by attributes ageTo and ageFrom, where ageFrom &lt;
 * ageTo so it always covers two years at least. One belongs to this category
 * iff (current_year - his_born_year) is between ageFrom and ageTo both
 * inclusive, ie. his age by the end of current year is within agecategory
 * bounds.
 *
 * @author Michal Prokeš, 72758
 */
@Entity
public class AgeCategory implements Serializable, Comparable<AgeCategory> {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(nullable = false)
    private String name;
    @Column(nullable = false)
    private int ageFrom;
    @Column(nullable = false)
    private int ageTo;

    /**
     * Default constructor without parameters
     */
    public AgeCategory() {
    }

    /**
     * Get unique ID
     *
     * @return
     */
    public Long getId() {
        return this.id;
    }

    /**
     * Set unique ID
     *
     * @param id unique id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * Get category name
     *
     * @return category name
     */
    public String getName() {
        return this.name;
    }

    /**
     * Set category name
     *
     * @param name category name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Minimal age of players in this category
     *
     * @return minimal age
     */
    public int getAgeFrom() {
        return this.ageFrom;
    }

    /**
     *
     * @param ageFrom minimal age of players
     */
    public void setAgeFrom(int ageFrom) {
        this.ageFrom = ageFrom;
    }

    /**
     *
     * @return maximal age of players
     */
    public int getAgeTo() {
        return this.ageTo;
    }

    /**
     *
     * @param ageTo maximal age of players
     */
    public void setAgeTo(int ageTo) {
        this.ageTo = ageTo;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || id == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final AgeCategory other = (AgeCategory) obj;
        if (this.id != other.id && (!this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 89 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return "AgeCategory{" + "id=" + id + ", name=" + name + ", ageFrom=" + ageFrom + ", ageTo=" + ageTo + '}';
    }

    /**
     * Compares this to another AgeCategory. Comparison is done
     * lexicographically on tuple (ageFrom, ageTo).
     *
     * <b>Note:</b> this method is not synchronized with equals.
     *
     * @param o age category to compare with
     * @return negative integer, zero or positive integer when tuple
     *         (ageFrom,ageTo) of this age category is lexicographically less than,
     *         equal to or greater than the tuple (ageFrom,ageTo of the other age
     *         category
     * @throws IllegalArgumentException when o is null
     */
    @Override
    public int compareTo(AgeCategory o) {
        if (o == null) {
            throw new IllegalArgumentException("Cannot compare to null");
        }

        if (this.ageFrom == o.ageFrom) {
            return this.ageTo - o.ageTo;
        } else {
            return this.ageFrom - o.ageFrom;
        }
    }
}